<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText" :titleExplain="titleExplain"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <template v-if="loginType == 'JC'">
          <el-form :inline="true" :model="filterField" ref="searchConditions">
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="所属地区"
                class="row-padding-bottom"
                v-if="loginType == 'JC'"
              >
                <el-cascader
                  ref="manageArea"
                  v-model="areaId"
                  :options="areasOptions"
                  @change="getAreaId"
                  clearable
                  filterable
                  :props="{ checkStrictly: true }"
                  :append-to-body="false"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="行业" class="row-padding-bottom">
                <el-select
                  v-model="filterField.industryId"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in industryOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <template v-if="advanced">
              <el-col :lg="8" :xl="6">
                <el-form-item label="建档日期" class="row-padding-bottom">
                  <!-- <el-date-picker
                            v-model="filingDate"
                            type="daterange"
                            unlink-panels
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            value-format="yyyy-MM-dd"
                        ></el-date-picker> -->
                  <timeSelection
                    @getStartTimeValue="getStartTimeValue"
                    @getEndTimeValue="getEndTimeValue"
                  ></timeSelection>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="开工状态" class="row-padding-bottom">
                  <el-select
                    v-model="filterField.status"
                    filterable
                    placeholder="请选择"
                    clearable
                  >
                    <el-option
                      v-for="item in statusOptions"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                      :popper-append-to-body="false"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="建设方" class="row-padding-bottom">
                  <el-input
                    v-model="filterField.pcName"
                    clearable
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="施工方" class="row-padding-bottom">
                  <el-input
                    v-model="filterField.pbName"
                    clearable
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="项目名称" class="row-padding-bottom">
                  <el-input
                    v-model="filterField.projectName"
                    placeholder="请输入项目名称"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="评价等级" class="row-padding-bottom">
                  <el-select
                    v-model="filterField.gradeLevel"
                    filterable
                    placeholder="请选择"
                    clearable
                    :popper-append-to-body="false"
                  >
                    <!-- <el-option v-for="item in levelOptions" :key="item.code" :label="item.name" :value="item.code"></el-option> -->
                    <el-option
                      v-for="(item, index) in selectLevel"
                      :key="index"
                      :label="item.name"
                      :value="item.gradeLevel"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <!-- <el-form-item>
                        <el-button
                            type="primary"
                            icon="icon iconfont iconchazhao1"
                            @click="searchList"
                            class="commonBtn"
                        ></el-button>
                    </el-form-item> -->
            </template>
            <el-col :lg="8" :xl="6">
              <el-form-item label="">
                <a
                  @click="toggleAdvanced"
                  style="margin-left: 40px; color: #409eff; cursor: pointer"
                  class="btnColor"
                >
                  {{ advanced ? '收起' : '展开' }}
                  <i
                    :class="
                      advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
                    "
                  ></i>
                </a>
                <el-button
                  type="primary"
                  icon="icon iconfont iconchazhao1"
                  @click="searchList"
                  class="searchBtn"
                >
                  <span>搜索</span>
                </el-button>
                <exportBtn
                  :dataLen="tableList.length"
                  :filterField="filterField"
                  :api="api"
                ></exportBtn>
              </el-form-item>
            </el-col>
          </el-form>
        </template>
        <template v-if="loginType != 'JC'">
          <el-form :inline="true" :model="filterField" ref="searchConditions">
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="所属地区"
                class="row-padding-bottom"
                v-if="loginType == 'JC'"
              >
                <el-cascader
                  ref="manageArea"
                  v-model="areaId"
                  :options="areasOptions"
                  @change="getAreaId"
                  clearable
                  filterable
                  :props="{ checkStrictly: true }"
                  :append-to-body="false"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="行业" class="row-padding-bottom">
                <el-select
                  v-model="filterField.industryId"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in industryOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="8" :xl="6">
              <el-form-item label="建档日期" class="row-padding-bottom">
                <!-- <el-date-picker
                            v-model="filingDate"
                            type="daterange"
                            unlink-panels
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            value-format="yyyy-MM-dd"
                        ></el-date-picker> -->
                <timeSelection
                  @getStartTimeValue="getStartTimeValue"
                  @getEndTimeValue="getEndTimeValue"
                ></timeSelection>
              </el-form-item>
            </el-col>
            <template v-if="advanced">
              <el-col :lg="8" :xl="6">
                <el-form-item label="开工状态" class="row-padding-bottom">
                  <el-select
                    v-model="filterField.status"
                    filterable
                    placeholder="请选择"
                    clearable
                    :popper-append-to-body="false"
                  >
                    <el-option
                      v-for="item in statusOptions"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="建设方" class="row-padding-bottom">
                  <el-input
                    v-model="filterField.pcName"
                    clearable
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="施工方" class="row-padding-bottom">
                  <el-input
                    v-model="filterField.pbName"
                    clearable
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="项目名称" class="row-padding-bottom">
                  <el-input
                    v-model="filterField.projectName"
                    placeholder="请输入项目名称"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="评价等级" class="row-padding-bottom">
                  <el-select
                    v-model="filterField.gradeLevel"
                    filterable
                    placeholder="请选择"
                    clearable
                    :popper-append-to-body="false"
                  >
                    <!-- <el-option v-for="item in levelOptions" :key="item.code" :label="item.name" :value="item.code"></el-option> -->
                    <el-option
                      v-for="(item, index) in selectLevel"
                      :key="index"
                      :label="item.name"
                      :value="item.gradeLevel"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <!-- <el-form-item>
                        <el-button
                            type="primary"
                            icon="icon iconfont iconchazhao1"
                            @click="searchList"
                            class="commonBtn"
                        ></el-button>
                    </el-form-item> -->
            </template>
            <el-col :lg="8" :xl="6">
              <el-form-item label="">
                <a
                  @click="toggleAdvanced"
                  style="margin-left: 40px; color: #409eff; cursor: pointer"
                  class="btnColor"
                >
                  {{ advanced ? '收起' : '展开' }}
                  <i
                    :class="
                      advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
                    "
                  ></i>
                </a>
                <el-button
                  type="primary"
                  icon="icon iconfont iconchazhao1"
                  @click="searchList"
                  class="searchBtn"
                >
                  <span>搜索</span>
                </el-button>
                <exportBtn
                  :dataLen="tableList.length"
                  :filterField="filterField"
                  :api="api"
                ></exportBtn>
              </el-form-item>
            </el-col>
          </el-form>
        </template>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          border
          style="border: 1px solid #dfe6ec"
          @sort-change="sortChange"
          @header-dragend="handleDragend"
        >
          <!-- <el-table-column prop="projectName" label="项目名称"></el-table-column>  -->
          <el-table-column prop="projectName" label="项目名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="areaDetailDesc"
            label="地区"
            :formatter="formatTd"
            width="150"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.areaDetailDesc"
                :columnName="'areaDetailDesc'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column prop="pcName" label="建设方" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.pcName"
                :columnName="'pcName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column prop="pbName" label="施工方" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.pbName"
                :columnName="'pbName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="score"
            label="评价"
            sortable="custom"
            align="center"
            :formatter="formatTd"
            min-width="140"
          >
            <template slot-scope="scope">
              <span
                >{{ scope.row.score }} /
                {{
                  scope.row.gradeLevel === 'O' ? '其他' : scope.row.gradeLevel
                }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="projectCreateTime"
            label="建档日期"
            align="center"
            :formatter="formatTd"
            min-width="130"
            sortable="custom"
          ></el-table-column>
          <el-table-column
            prop="statusDesc"
            label="项目状态"
            align="center"
            :formatter="formatTd"
            min-width="120"
          ></el-table-column>
          <el-table-column label="操作" align="left" fixed="right" width="220">
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-if="isShowDetail"
                @click="toDetail(scope.$index, scope.row)"
                >查看详情</el-link
              >
              <!-- 信用历史评价 -->
              <el-link
                type="primary"
                v-if="isShowDetail"
                @click="toHistoryList(scope.row)"
                >查看历史评价</el-link
              >
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
    </div>
  </div>
</template>

<script>
import emptyTable from '@/components/publicCom/emptyTable'
import Pagination from '@/components/publicCom/pagination'
import topTitle from '@/components/publicCom/topTitle'
import { mapState, mapActions } from 'vuex'
import scrollTable from '@/mixin/scrollTable.js'

export default {
  name: 'projectScoreList',
  mixins: [scrollTable],
  components: {
    topTitle,
    emptyTable,
    Pagination,
  },
  data() {
    return {
      titleText: '项目评价',
      advanced: false,
      titleExplain:
        '针对项目实名制、专户等情况以及项目重大事件，对项目进行评价，频率为每天更新1次',
      levelOptions: [], //评价等级
      statusOptions: [], //状态
      //表格数据
      tableList: [],
      requestUrl: '',
      areaId: '', //区域
      //搜索条件
      filterField: {
        sysAppCode: localStorage.getItem('judgeRoleCode'),
        projectId: '',
        projectName: '', //项目名称
        areaId: '', //区域
        status: '', //开工状态
        industryId: '', //行业
        gradeLevel: '', //评价等级
        pcName: '', //建设方
        pbName: '', //施工方
        startDate: '',
        endDate: '',
        serverArea: '', // 服务部署区域
      },
      isShow: false, //列表加载
      loading: true,
      loginType: '',
      filingDate: [],
      selectLevel: [],
      isShowDetail: '',
      api: '',
    }
  },
  computed: {
    ...mapState(['areasOptions', 'industryOptions']),
  },
  created() {
    this.$store.state.activeMenu = '/home/enterpriseScore/projectScoreList'
    this.loginType = localStorage.getItem('loginType')
    if (this.loginType === 'QY' || this.loginType === 'XM') {
      this.requestUrl =
        'api-ps/creditProjectScore/expand/getProjectScoreListByCompany'
    } else {
      this.requestUrl =
        'api-ps/creditProjectScore/expand/getProjectScoreListByMonitor'
      //获取区域
      this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
      if (this.$route.query.type == 'gradeLevelModular') {
        this.areaId = this.$route.query.areaId
        this.filterField.areaId = this.$route.query.areaId
        this.filterField.status = this.$route.query.status
        this.filterField.gradeLevel = this.$route.query.gradeLevel
      }
    }
    // 开工状态
    this.getStatus()
    this.getLevelList('20') //项目评级配置
    //获取行业
    this.getIndustry({})
    this.getProject()
    this.getApi()
    this.filterField.serverArea = this.$store.state.areaInfoArr.areaId
    this.isShowDetail =
      this.$store.state.buttonLimitsArr.indexOf('projectScoreList:detail') >
        -1 ||
      this.$store.state.buttonLimitsArr.indexOf('projectScoreListSGF:detail') >
        -1 ||
      this.$store.state.buttonLimitsArr.indexOf(
        'projectEvaluationListJSF:detail'
      ) > -1
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    ...mapActions(['getAreas', 'getIndustry']),
    /**
     * @description: 获取开始日期
     * @param {*} urls
     * @param {*} datas
     */
    getStartTimeValue(val) {
      if (val) {
        this.filterField.startDate = val
      } else {
        this.filterField.startDate = ''
      }
    },
    /**
     * @description: 获取结束日期
     * @param {*} urls
     * @param {*} datas
     */
    getEndTimeValue(val) {
      if (val) {
        this.filterField.endDate = val
      } else {
        this.filterField.endDate = ''
      }
    },
    getProject() {
      if (Object.keys(this.$route.query).length > 0) {
        this.filterField.projectName = this.$route.query.projectName
          ? decodeURI(this.$route.query.projectName)
          : ''
      }
      this.$nextTick(function () {
        this.$refs.pagination.getTableListData()
      })
    },
    // 获取评级配置
    getLevelList(type) {
      return this.axios({
        method: 'post',
        url: 'api-ps/creditLevel/expand/getCreditLevelList',
        data: {
          type,
        },
      })
        .then((response) => {
          if (!response.data.hasError) {
            const results = response.data.result
            results.forEach((v) => {
              v.name = v.gradeLevel
            })
            this.selectLevel = results
            const otherLevel = { gradeLevel: 'O', name: '其他' }
            this.selectLevel.push(otherLevel)
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 获取评价等级和开工状态
    getStatus() {
      this.axios({
        method: 'post',
        url: '/api-ps/creditProjectScore/expand/findprowashbytype',
        data: { type: 'PROJECT_STATUS' },
      })
        .then((res) => {
          if (!res.data.hasError) {
            this.statusOptions = res.data.result.filter(
              (v) => v.code != 'NOT_STARTED'
            )
          } else {
            this.$message({
              showClose: true,
              duration: 3000,
              message: res.data.errorMsg,
              type: 'error',
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '请求失败',
            type: 'error',
          })
        })
    },
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    //跳转详情
    toDetail(index, row) {
      let path = '/home/enterpriseScore/projectScoreDetail'
      let name = 'projectScoreDetail'
      if (this.isEsUser()) {
        path = '/home/enterpriseScore/ESprojectScoreDetail'
        name = 'ESprojectScoreDetail'
      }
      this.$router.push({
        path,
        name,
        query: {
          projectId: row.projectId,
          projectScoreId: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/enterpriseScore/projectScoreList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    // 历史评价
    toHistoryList(row) {
      this.$router.push({
        path: '/home/enterpriseScore/projectHistoryScoreList',
        name: 'projectHistoryScoreList',
        query: {
          projectId: row.projectId,
          projectScoreId: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/enterpriseScore/projectScoreList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    getApi() {
      let loginType = localStorage.getItem('loginType')
      if (loginType === 'JC') {
        this.api = 'api-s/report/export_JC_CS_PROJECT_SCORE'
      } else if (loginType === 'QY') {
        this.api = 'api-s/report/export_QY_PROJECT_SCORE'
      } else if (loginType === 'XM') {
        this.api = 'api-s/report/export_XM_PROJECT_SCORE'
      }
    },
  },
}
</script>

<style scoped lang="less">
@import '../../../static/css/filterArea.less';
</style>
